package cn.com.nes.common.util;

import cn.com.nes.site.service.comecall.dto.QueryOrderDescDto;
import cn.com.nes.site.service.comecall.dto.faultAndWarnDescDto;
import cn.hutool.core.util.ObjectUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: nes-call-center
 * @Date: 2019/2/22 10:56
 * @Author: shihx
 * @Description:
 */
public class DateContUtil {


    /**
     * get Calendar of given year
     * @param year
     * @return
     */
    private static Calendar getCalendarFormYear(int year){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.YEAR, year);
        return cal;
    }

    /**
     * get start date of given week no of a year
     * @param year
     * @param weekNo
     * @return
     */
    public static String getStartDayOfWeekNo(int year,int weekNo){
        Calendar cal = getCalendarFormYear(year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
                cal.get(Calendar.DAY_OF_MONTH);

    }

    /**
     * get the end day of given week no of a year.
     * @param year
     * @param weekNo
     * @return
     */
    public static String getEndDayOfWeekNo(int year,int weekNo){
        Calendar cal = getCalendarFormYear(year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
                cal.get(Calendar.DAY_OF_MONTH);
    }


    /**
    * @Description:根据一年内的第几周计算日期
    * @param :
    * @Return:
    * @Author: shihx
    * @Date: 2019/2/22 11:18
    */
    public static String getStartAndEndTime(int year,String weekNo){

        if(weekNo.substring(0,1).equals("0")){
            weekNo=weekNo.substring(1);
        }

        return DateContUtil.getStartDayOfWeekNo(year,Integer.parseInt(weekNo))+"_"+DateContUtil.getEndDayOfWeekNo(year,Integer.parseInt(weekNo));
    }

    /**
    * @Description: 计算上周日的日期
    * @param :
    * @Return:
    * @Author: shihx
    * @Date: 2019/2/22 11:23
    */
    public static String getTopWeekTime(){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_WEEK,1);
        SimpleDateFormat f =new SimpleDateFormat("yyyy-MM-dd");
        return f.format(c.getTime());
    }

    //获取某个日期的前几天日期
    public static String getTopDayDate(int day,String time) {
        SimpleDateFormat f =new SimpleDateFormat("yyyy-MM-dd");
        try {
            System.out.println(time);
            Date date = f.parse(time);
            System.out.println(date+" "+time);
            return f.format(new Date(date.getTime() - (long)day * 24 * 60 * 60 * 1000));
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取某个日期的 前几年 -1、后几年日期
     * @param year
     * @param time yyyy-MM-dd HH:mm:ss
     */
    public static Date getTopYearDate(int year,String time) {
        SimpleDateFormat f = Globals.DATE_FORMAT_LONG;
        try {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(f.parse(time));
            calendar.add(calendar.YEAR, year);
            return calendar.getTime();
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
    * @Description: 获取上个月月末日期
    * @param ：
    * @Return:
    * @Author: shihx
    * @Date: 2019/2/22 13:09
    */
    public static String getTopMonth(){
        SimpleDateFormat f =new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        //c.set(Calendar.MONTH,0);

        int lastMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),lastMaxDay);
        return f.format(c.getTime());
    }

    /**
    * @Description: 获取指定日期的前几个月的第一天日期
    * @param :
    * @Return: 
    * @Author: shihx
    * @Date: 2019/2/22 13:26
    */
    public static String getMonthTime(int num,String time){
        try {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(f.parse(time));
            c.add(Calendar.MONTH, -num);
            c.set(Calendar.DAY_OF_MONTH, 1);
            return f.format(c.getTime());
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 补全统计数据时间
     * @param startTime 开始时间
     * @param num 统计时长
     * @param list 统计数据
     * @param type 统计类型
     */
    public static List<QueryOrderDescDto> supplement(String startTime, Integer num, List<QueryOrderDescDto> list,
                                                     Integer type) throws ParseException {
        int day = 0;
        int week = 1;
        int month = 2;
        boolean flag = true;
        Iterator iterator = list.iterator();
        QueryOrderDescDto dto = new QueryOrderDescDto();
        if (ObjectUtil.isNull(list) || list.size()==0) {
            flag = false;
        } else {
            dto = (QueryOrderDescDto) iterator.next();
        }
        List<QueryOrderDescDto> resList = new ArrayList<>();
        //数据按日统计
        if (day == type) {
            //处理开始时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(startTime));
            for (int i = 0; i < num; i++) {
                if (flag && format.format(calendar.getTime()).equals(dto.getDate())) {
                    //统计数据有当前日期
                    resList.add(dto);
                    if (iterator.hasNext()) {
                        dto = (QueryOrderDescDto) iterator.next();
                    }
                } else {
                    //统计数据无当前日期
                    QueryOrderDescDto tempDto = new QueryOrderDescDto();
                    tempDto.setDate(format.format(calendar.getTime()));
                    tempDto.setCompleteWorkOrderCon(0);
                    tempDto.setCreateWorkOrderCon(0);
                    resList.add(tempDto);
                }
                //日期加一
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
        //数据按周统计
        if (week == type) {
            //处理开始时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(startTime));
            Calendar end = Calendar.getInstance();
            end.setTime(format.parse(startTime));
            end.add(Calendar.DAY_OF_MONTH, 6);
            String cweek = format.format(calendar.getTime()) + "_"
                    + format.format(end.getTime());
            for (int i = 0; i < num; i++) {
                if (flag && cweek.equals(dto.getDate())) {
                    //统计数据有当前日期
                    resList.add(dto);
                    if (iterator.hasNext()) {
                        dto = (QueryOrderDescDto) iterator.next();
                    }
                } else {
                    //统计数据无当前日期
                    QueryOrderDescDto tempDto = new QueryOrderDescDto();
                    tempDto.setDate(cweek);
                    tempDto.setCompleteWorkOrderCon(0);
                    tempDto.setCreateWorkOrderCon(0);
                    resList.add(tempDto);
                }
                //日期加七
                calendar.add(Calendar.DAY_OF_MONTH, 7);
                end.add(Calendar.DAY_OF_MONTH, 7);
                cweek = format.format(calendar.getTime()) + "_" + format.format(end.getTime());
            }
        }
        //数据按月统计
        if (month == type) {
            //处理开始时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(startTime));
            for (int i = 0; i < num; i++) {
                if (flag && format.format(calendar.getTime()).equals(dto.getDate())) {
                    //统计数据有当前日期
                    resList.add(dto);
                    if (iterator.hasNext()) {
                        dto = (QueryOrderDescDto) iterator.next();
                    }
                } else {
                    //统计数据无当前日期
                    QueryOrderDescDto tempDto = new QueryOrderDescDto();
                    tempDto.setDate(format.format(calendar.getTime()));
                    tempDto.setCompleteWorkOrderCon(0);
                    tempDto.setCreateWorkOrderCon(0);
                    resList.add(tempDto);
                }
                //日期加七
                calendar.add(Calendar.MONTH, 1);
            }
        }
        return resList;
    }

    /**
     * 补全统计数据时间
     * @param startTime 开始时间
     * @param num 统计时长
     * @param list 统计数据
     * @param type 统计类型
     */
    public static List<faultAndWarnDescDto> supplementWarnAndFault(String startTime, Integer num, List<faultAndWarnDescDto> list,
                                                                   Integer type) throws ParseException {
        int day = 0;
        int week = 1;
        int month = 2;
        boolean flag = true;
        Iterator iterator = list.iterator();
        faultAndWarnDescDto dto = new faultAndWarnDescDto();
        if (ObjectUtil.isNull(list) || list.size()==0) {
            flag = false;
        } else {
            dto = (faultAndWarnDescDto) iterator.next();
        }
        List<faultAndWarnDescDto> resList = new ArrayList<>();
        //数据按日统计
        if (day == type) {
            //处理开始时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(startTime));
            for (int i = 0; i < num; i++) {
                if (flag && format.format(calendar.getTime()).equals(dto.getDate())) {
                    //统计数据有当前日期
                    resList.add(dto);
                    if (iterator.hasNext()) {
                        dto = (faultAndWarnDescDto) iterator.next();
                    }
                } else {
                    //统计数据无当前日期
                    faultAndWarnDescDto tempDto = new faultAndWarnDescDto();
                    tempDto.setDate(format.format(calendar.getTime()));
                    tempDto.setNum(0);
                    resList.add(tempDto);
                }
                //日期加一
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
        //数据按周统计
        if (week == type) {
            //处理开始时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(startTime));
            Calendar end = Calendar.getInstance();
            end.setTime(format.parse(startTime));
            end.add(Calendar.DAY_OF_MONTH, 6);
            String cweek = format.format(calendar.getTime()) + "_"
                    + format.format(end.getTime());
            for (int i = 0; i < num; i++) {
                if (flag && cweek.equals(dto.getDate())) {
                    //统计数据有当前日期
                    resList.add(dto);
                    if (iterator.hasNext()) {
                        dto = (faultAndWarnDescDto) iterator.next();
                    }
                } else {
                    //统计数据无当前日期
                    faultAndWarnDescDto tempDto = new faultAndWarnDescDto();
                    tempDto.setDate(cweek);
                    tempDto.setNum(0);
                    resList.add(tempDto);
                }
                //日期加七
                calendar.add(Calendar.DAY_OF_MONTH, 7);
                end.add(Calendar.DAY_OF_MONTH, 7);
                cweek = format.format(calendar.getTime()) + "_" + format.format(end.getTime());
            }
        }
        //数据按月统计
        if (month == type) {
            //处理开始时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(startTime));
            for (int i = 0; i < num; i++) {
                if (flag && format.format(calendar.getTime()).equals(dto.getDate())) {
                    //统计数据有当前日期
                    resList.add(dto);
                    if (iterator.hasNext()) {
                        dto = (faultAndWarnDescDto) iterator.next();
                    }
                } else {
                    //统计数据无当前日期
                    faultAndWarnDescDto tempDto = new faultAndWarnDescDto();
                    tempDto.setDate(format.format(calendar.getTime()));
                    tempDto.setNum(0);
                    resList.add(tempDto);
                }
                //日期加七
                calendar.add(Calendar.MONTH, 1);
            }
        }
        return resList;
    }

    /**
     * 获取两个日期相差多少个月
     */
    public static int getMonthSpace(String date1, String date2)
            throws ParseException {
        int result = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));
        int i = c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        int month = 0;
        if (i<0)
        {
            month = -i*12;
        }else if(i>0)
        {
            month =  i*12;
        }
        result = (c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH)) + month;
        return result == 0 ? 1 : Math.abs(result) +1;
    }

    /**
     * 获取两个日期相差多少天
     * star  end  "yyyy-MM-dd"
     */
    public static int getDaySpace(String star, String end) throws ParseException {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date starDay = dft.parse(star);//开始时间
        Date endDay = dft.parse(end);//结束时间
        Date nextDay = starDay;
        int i = 1;
        while (nextDay.before(endDay)) {//当明天不在结束时间之前是终止循环
            Calendar cld = Calendar.getInstance();
            cld.setTime(starDay);
            cld.add(Calendar.DATE, 1);
            starDay = cld.getTime();
            //获得下一天日期字符串
            nextDay = starDay;
            i++;
        }
        return i;
    }



    public static void main (String args[]){
       /* SimpleDateFormat f =new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(DateContUtil.getTopDayDate(1,f.format(new Date())));*/
      /* String date="2019-99";
       System.out.println(DateContUtil.getStartAndEndTime(Integer.parseInt(date.substring(0,4)),date.substring(5,7)));*/
//       try {
//            String date = "2019-02-23";
//            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
//            Date time = f.parse(date);
//            System.out.println(time.getTime());
//        }catch(Exception e){
//            e.printStackTrace();
//        }

      /*  String endTime ="2019-01-21";//上个月月末日期
        String startTime = DateContUtil.getMonthTime(6,endTime);//上个月月末往前几个月的月初日期
        System.out.println(startTime);
*/
        SimpleDateFormat f =new SimpleDateFormat("yyyy-MM-dd");
        String endTime = DateContUtil.getTopDayDate(1,f.format(new Date()));//昨天
        String endTime1 = DateContUtil.getTopDayDate(-1,f.format(new Date()));//明天
        String startTime = DateContUtil.getTopDayDate(7,endTime);//昨天往前的天数
        System.out.println(endTime);
        System.out.println(startTime);
    }

}



















